#include "ui_test.hpp"
#include "content/bindable.hpp"
#include "service/render_service.hpp"
#include "service/svc_manager.hpp"
#include <imgui.h>
#include <imgui_stdlib.h>
#include "imgui_ext/imgui_ext.h"
#include "text/IconsFontAwesome6.h"
#include <unordered_map>

/**
 * 支持的数据类型有：s8/s16/s32/s64 u8/u16/u32/u64 f32/f64 bool std::string c_string_t callback_t enumerate_t image ui
 * 
 */

static const char* typenames[] {
            "有符号整数(8位)",
            "有符号整数(16位)",
            "有符号整数(32位)",
            "有符号整数(64位)",
            "无符号整数(8位)",
            "无符号整数(16位)",
            "无符号整数(32位)",
            "无符号整数(64位)",
            "单精度浮点数",
            "双精度浮点数",
            "布尔值",
            "字符串(变长)",
            "字符串(定长)",
            "回调函数",
            "列举项",
            "图片",
            "视图",
        };
static const int type_count = std::size(typenames);

struct ui_bind_data: bindable_base {
    std::optional<std::tuple<value_type, std::any>> get_data(const std::string &key) override
    {

        return {};
    }

    void add(std::string &key, int type) {
        last_type = type;
        key_store = &key;
        ImGui::OpenPopup("添加数据");
    }

    void show_add_wizard()
    {
        if (ImGui::BeginPopup("添加数据"))
        {
            ImGui::TextUnformatted("类型：");
            ImGui::SameLine();
            ImGui::TextColored(ImVec4(0.2f,0.9f,0.15f,1.f), "%s", typenames[last_type]);
            ImGui::TextUnformatted("名称：");
            ImGui::SameLine();
            ImGui::TextColored(ImVec4(0.2f,0.9f,0.15f,1.f), "%s", key_store->c_str());
            ImGui::SeparatorText("参数设置:");
            if (ImGui::Button("确定")) {
                key_store->clear();
                last_type = -1;
                key_store = nullptr;
                ImGui::CloseCurrentPopup();
            }
            ImGui::SameLine();
            if (ImGui::Button("取消")) {
                ImGui::CloseCurrentPopup();
            }
            ImGui::EndPopup();
        }
    }

    // std::unordered_map<std::string, class Ty>
    int last_type;
    std::string *key_store;
};



ui_view* load_ui_file(const std::string &path)
{
    return ServiceManager::instance()->request_service<IRenderService>()->create_view_from_file(path);
}

void show_ui_test_data(bindable_base *&data, const char *view_id)
{
    if (!view_id)
        return;

    if (!data) {
        data = new ui_bind_data;
    }

    ui_bind_data *d = dynamic_cast<ui_bind_data*>(data);
    if (!d)
        return;

    using namespace ImGui;

    CenterWindowPercent(0.65f);
    if (BeginPopupModal(view_id, nullptr, ImGuiWindowFlags_NoResize|ImGuiWindowFlags_NoMove)) {
        static int data_idx = 0;
        static std::string key;
        
        AlignTextToFramePadding();
        TextUnformatted("数据名称：");
        SameLine();
        SetNextItemWidth(220);
        InputText("##KEY", &key);
        SameLine(0, GetFontSize()*2.f);
        TextUnformatted("数据类型：");
        SameLine();
        SetNextItemWidth(280);
        if (BeginCombo("##TYPE", typenames[data_idx])) {
            for (int i=0; i<type_count;++i) {
                if (Selectable(typenames[i], i == data_idx))
                    data_idx = i;
            }
            EndCombo();
        }
        SameLine();
        BeginDisabled(key.empty());
        if (Button(ICON_FA_PLUS " 添加")) {
            d->add(key, data_idx);
        }
        EndDisabled();

        d->show_add_wizard();
        
        BeginChild("##DataTable", ImVec2(0, -GetTextLineHeightWithSpacing()*1.75f));

        EndChild();

        if (BottomButtons({ICON_FA_XMARK " 关闭"}) > -1)
            CloseCurrentPopup();
        EndPopup();
    }
}

void load_ui_test_data(bindable_base *&data)
{
    if (!data) {
        data = new ui_bind_data;
    } else if (!dynamic_cast<ui_bind_data*>(data)) {
        return;
    }
}

void store_ui_test_data(bindable_base *data)
{
    if (!data || !dynamic_cast<ui_bind_data*>(data)) {
        return;
    }
}
